<?php


namespace app\admin\controller\mall;

use app\admin\model\MallGoods;
use app\admin\model\MallOrderForm;
use app\admin\model\MallMerchantOrders;
use app\admin\traits\Curd;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\exception\ValidateException;

/**
 * Class Goods
 * @package app\admin\controller\mall
 * @ControllerAnnotation(title="商城商品管理")
 */
class OrderForm extends AdminController
{

    use Curd;

    protected $relationSearch = true;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new MallOrderForm();
        $this->model2 = new MallMerchantOrders();
        $this->model3 = new MallGoods();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->withJoin('cate', 'LEFT')
                ->where($where)
                ->count();
            $list = $this->model
                ->withJoin('cate', 'LEFT')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $list = status($list);
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            // echo '<pre>';
            // print_r(json($data));die;
            return json($data);
        }
        return $this->fetch();
    }

    public function add()
    {
        try {
            if ($this->request->isAjax()) {
                if (input('selectFields')) {
                    return $this->selectList();
                }
                list($page, $limit, $where) = $this->buildTableParames();
                $count = $this->model
                    ->withJoin('cate', 'LEFT')
                    ->where($where)
                    ->count();
                $list = $this->model
                    ->withJoin('cate', 'LEFT')
                    ->where($where)
                    ->page($page, $limit)
                    ->order($this->sort)
                    ->select();
                $list = $list->toArray();
                $edit_Status = edit_Status($list);
                // echo '<pre>';
                // print_r($edit_Status);
                // die;

                //状态修改成功
                if ($edit_Status == true) {
                    MallMerchantOrders::createOrder($edit_Status);
                    $data = [
                        'code'  => 0,
                        'msg'   => '拆分成功',
                        'count' => $count,
                        'data'  => $list,
                    ];
                    return json($data);
                }
            }
        } catch (\Exception $e) {
            return '新增订单失败：' . $e->getMessage();
        }

        // return $this->fetch();
    }
}
